package com.ruoyi.web.controller.app.api.controller;

import com.ruoyi.applet.appletReceivingAddress.domain.AppletReceivingAddress;
import com.ruoyi.applet.appletReceivingAddress.service.IAppletReceivingAddressService;
import com.ruoyi.applet.entity.PageR;
import com.ruoyi.applet.entity.ReceivingAddressApplet;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收货地址接口
 * @author ruoyi
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/wxApi/receivingAddress")
public class ReceivingAddressAppletController extends BaseController {
    
    @Resource
    private IAppletReceivingAddressService appletReceivingAddressService;
    
    /**
     * 查询收货地址
     */
    @PostMapping("/getReceivingAddress")
    public AjaxResult getRegionList(@RequestBody ReceivingAddressApplet receivingAddressApplet) {
        Long wxUserId = receivingAddressApplet.getWxUserId();
        if (wxUserId == null) return AjaxResult.error("请先登录！");
        
        PageR<AppletReceivingAddress> appletReceivingAddresses = appletReceivingAddressService.selectAppletReceivingAddressListApplet(receivingAddressApplet);
        return AjaxResult.success(appletReceivingAddresses);
    }
    
    /**
     * 查询默认收货地址
     */
    @PostMapping("/getDefaultReceivingAddress")
    public AjaxResult getDefaultReceivingAddress(@RequestBody AppletReceivingAddress receivingAddress) {
        Long wxUserId = receivingAddress.getWxUserId();
        if (wxUserId == null) return AjaxResult.warn("请先登录！");
        receivingAddress.setIsDefault(1);
        List<AppletReceivingAddress> appletReceivingAddresses = appletReceivingAddressService.selectAppletReceivingAddressList(receivingAddress);
        return AjaxResult.success(appletReceivingAddresses);
    }
    
    /**
     * 添加收货地址
     */
    @PostMapping("/addReceivingAddress")
    public AjaxResult addReceivingAddress(@RequestBody AppletReceivingAddress receivingAddress) {
        Long wxUserId = receivingAddress.getWxUserId();
        if (wxUserId == null) return AjaxResult.warn("请先登录！");
        int i = appletReceivingAddressService.insertAppletReceivingAddress(receivingAddress);
        return AjaxResult.success(i);
    }
    
    /**
     * 修改收货地址
     */
    @PostMapping("/updateReceivingAddress")
    public AjaxResult updateReceivingAddress(@RequestBody AppletReceivingAddress receivingAddress) {
        Long wxUserId = receivingAddress.getWxUserId();
        if (wxUserId == null) return AjaxResult.warn("请先登录！");
        Long id = receivingAddress.getId();
        if (id == null) return AjaxResult.warn("请选择收货地址！");
        int i = appletReceivingAddressService.updateAppletReceivingAddress(receivingAddress);
        return AjaxResult.success(i);
    }
    
    /**
     * 删除收货地址
     */
    @PostMapping("/delReceivingAddress")
    public AjaxResult delReceivingAddress(@RequestBody AppletReceivingAddress receivingAddress) {
        Long wxUserId = receivingAddress.getWxUserId();
        if (wxUserId == null) return AjaxResult.warn("请先登录！");
        int i = appletReceivingAddressService.deleteAppletReceivingAddressById(receivingAddress.getId());
        return AjaxResult.success(i);
    }
    
    /**
     * 获取收货地址信息
     */
    @PostMapping("/getOneReceivingAddress")
    public AjaxResult getOneReceivingAddress(@RequestBody AppletReceivingAddress receivingAddress) {
        Long wxUserId = receivingAddress.getWxUserId();
        if (wxUserId == null) return AjaxResult.warn("请先登录！");
        Long id = receivingAddress.getId();
        if (id == null) return AjaxResult.warn("请选择收货地址！");
        AppletReceivingAddress appletReceivingAddress = appletReceivingAddressService.selectAppletReceivingAddressById(receivingAddress.getId());
        return AjaxResult.success(appletReceivingAddress);
    }
}
